<?php

/**
 *    我的收货地址控制器
 *
 *    @author    Garbin
 *    @usage    none
 */
class My_addressApp extends MemberbaseApp
{
    function index()
    {
        /* 取得列表数据 */
        $addr_id = empty($_GET['addr_id']) ? 0 : intval($_GET['addr_id']);
        $model_address =& m('address');
        $addresses     = $model_address->find(array(
            'conditions'    => 'user_id = ' . $this->visitor->get('user_id'),
            'order'    => 'is_deafult DESC, addr_id DESC',
        ));
        $this->assign('addresses_list', $addresses);

        /* 当前位置 */
        $this->_curlocal(LANG::get('member_center'), 'index.php?app=member',
                         LANG::get('my_address'), 'index.php?app=my_address',
                         LANG::get('address_list'));

        /* 当前用户中心菜单 */
        $this->_curitem('my_address');

        /* 当前所处子菜单 */
        $this->_curmenu('address_list');

        $this->import_resource(array(
                'script' => 'jquery.plugins/jquery.validate.js,jquery.plugins/additional-methods.js,mlselection.js',
            ));
        if($_GET['act_type']=='edit' && $addr_id !=0)
        {
            $find_data     = $model_address->find("addr_id = {$addr_id} AND user_id=" . $this->visitor->get('user_id'));
            if (empty($find_data))
            {
                echo Lang::get('no_such_address');

                return;
            }
             $address = current($find_data);
             if($address['phone_tel'])
             {
                $phone_tel = explode('-', $address['phone_tel']);
             }
             $this->assign('act', 'edit');
             $this->assign('phone_tel', $phone_tel);
             $this->assign('address', $address);
        }
        else
        {
             $this->assign('act', 'add');
        }    
        $this->_config_seo('title', Lang::get('buyer_center') . ' - ' . Lang::get('my_address'));
        $this->_get_regions();
        $this->display('my_address.index.html');
    }

    /**
     *    添加地址
     *
     */
    function add()
    {
        
        if (!IS_POST)
        {
            $this->show_message('no_valid_data','back_list', 'index.php?app=my_address');
        }
        else
        {
            if (!$_POST['consignee'] || !$_POST['region_name'] || !$_POST['address'] || (!$_POST['phone_mob'] && !$_POST['phone_code']))
            {

               $this->show_warning('数据不完整!请重新填写!');
                return;
            }

            if ((!is_mobile($_POST['phone_mob']) && $_POST['phone_mob'])  || (!is_zipcode($_POST['zipcode']) && $_POST['zipcode']))
            {

               $this->show_warning('format_error');
                return;
            }
           
            

            $data = array(
                'user_id'       => $this->visitor->get('user_id'),
                'consignee'     => html_script(trim($_POST['consignee'])),
                'region_id'     => (int)$_POST['region_id'],
                'region_name'   => html_script(trim($_POST['region_name'])),
                'address'       => html_script(trim($_POST['address'])),
                'zipcode'       => html_script(trim($_POST['zipcode'])),                
                'phone_mob'     => html_script(trim($_POST['phone_mob'])),
                'is_deafult'     => isset($_POST['default']) ? (int)$_POST['default'] : 0 ,
                
            );
             if($_POST['phone_code'] && $_POST['phone_section'])
            {
                if($_POST['phone_ext'])
                {
                     $data['phone_tel'] = trim($_POST['phone_section']).'-'.trim($_POST['phone_code']).'-'.trim($_POST['phone_ext']);
                }
                else
                {
                    $data['phone_tel'] = trim($_POST['phone_section']).'-'.trim($_POST['phone_code']);
                }
                if (!is_tel($data['phone_tel']))
                {  
                    $this->show_warning('电话号码不正确');
                    return;
                }              
            }
            else
            {
                $data['phone_tel'] = '';
            }
            $model_address =& m('address'); 
            $sql = "SELECT COUNT(*) FROM {$model_address->table} WHERE `user_id` = " . $this->visitor->get('user_id');     
            if((int)$model_address->getOne($sql)>=10)
            {
              $this->show_warning('add_address_error');
              return;  
            }
            
            if (!($address_id = $model_address->add($data)))
            {
                $this->show_warning($model_address->get_error());

                return;
            }
            if($data['is_deafult'] == 1)
            {
                $this->_set_default_address($address_id);
                
            }
            
          $this->show_message('add_address_successed','back_list', 'index.php?app=my_address');
        }
    }
    
    /**
     *    编辑地址
     *
     */
    function edit()
    {
        $addr_id = empty($_GET['addr_id']) ? 0 : intval($_GET['addr_id']);
        if (!IS_POST || !$addr_id)
        {
            $this->show_message('no_valid_data','back_list', 'index.php?app=seller_address&act=returns');                       
        }
        else
        {
            if (!$_POST['consignee'] || !$_POST['region_name'] || !$_POST['address'] || (!$_POST['phone_mob'] && !$_POST['phone_code']))
            {

               $this->show_warning('数据不完整!请重新填写!');
                return;
            }

            if ((!is_mobile($_POST['phone_mob']) && $_POST['phone_mob'])  || (!is_zipcode($_POST['zipcode']) && $_POST['zipcode']))
            {

               $this->show_warning('format_error');
                return;
            }
            
            $data = array(
                'consignee'     => html_script(trim($_POST['consignee'])),
                'region_id'     => (int)$_POST['region_id'],
                'region_name'   => html_script(trim($_POST['region_name'])),
                'address'       => html_script(trim($_POST['address'])),
                'zipcode'       => html_script(trim($_POST['zipcode'])),
                'phone_mob'     => html_script(trim($_POST['phone_mob'])),
                'is_deafult'     => isset($_POST['default']) ? (int)$_POST['default'] : 0 ,

            );
            if($_POST['phone_code'] && $_POST['phone_section'])
            {
                if($_POST['phone_ext'])
                {
                     $data['phone_tel'] = trim($_POST['phone_section']).'-'.trim($_POST['phone_code']).'-'.trim($_POST['phone_ext']);
                }
                else
                {
                    $data['phone_tel'] = trim($_POST['phone_section']).'-'.trim($_POST['phone_code']);
                }
                if (!is_tel($data['phone_tel']))
                {  
                    $this->show_warning('电话号码不正确');
                    return;
                }
              
            }
            else
            {
                $data['phone_tel'] = '';
            }
            $model_address =& m('address');
            $model_address->edit("addr_id = {$addr_id} AND user_id=" . $this->visitor->get('user_id'), $data);
            if ($model_address->has_error())
            {
                $this->show_warning($model_address->get_error());

                return;
            }
            if($data['is_deafult'] == 1)
            {
                $this->_set_default_address($addr_id);
                
            }
                                    
            $this->show_message('edit_address_successed','back_list', 'index.php?app=my_address');

        }
    }
    
    /**
     *    设为默认地址
     *
     */
    function setdefault()
    {
        $addr_id = empty($_GET['addr_id']) ? 0 : intval($_GET['addr_id']);
        if (!$addr_id)
        {
            echo Lang::get("no_such_address");
            return;
        }
        
        $model_address =& m('address');
        $model_address->edit("addr_id = {$addr_id} AND user_id=" . $this->visitor->get('user_id'), array('is_deafult'=>1));
        if ($model_address->has_error())
        {
            $this->show_warning($model_address->get_error());

            return;
        }
        $this->_set_default_address($addr_id);
        $this->show_message('set_default_successed');
        
    }
    
    /**
     *    删除地址
     *
     */
    function drop()
    {
        $addr_id = isset($_GET['addr_id']) ? trim($_GET['addr_id']) : 0;
        if (!$addr_id)
        {
            $this->show_warning('no_such_address');

            return;
        }
        $ids = explode(',', $addr_id);//获取一个类似array(1, 2, 3)的数组
        $model_address  =& m('address');
        $drop_count = $model_address->drop("user_id = " . $this->visitor->get('user_id') . " AND addr_id " . db_create_in($ids));
        if (!$drop_count)
        {
            /* 没有可删除的项 */
            $this->show_warning('no_such_address');

            return;
        }

        if ($model_address->has_error())    //出错了
        {
            $this->show_warning($model_address->get_error());

            return;
        }

        $this->show_message('drop_address_successed');
    }
    
    /**
     *    获取地区信息
     *
     */
    function _get_regions()
    {
        $model_region =& m('region');
        $regions = $model_region->get_list(0);
        if ($regions)
        {
            $tmp  = array();
            foreach ($regions as $key => $value)
            {
                $tmp[$key] = $value['region_name'];
            }
            $regions = $tmp;
        }
        $this->assign('regions', $regions);
    }
    /**
     *    三级菜单
     *
     *    @author    Garbin
     *    @return    void
     */
    function _get_member_submenu()
    {
        $menus = array(
            array(
                'name'  => 'address_list',
                'url'   => 'index.php?app=my_address',
            ),
/*            array(
                'name'  => 'add_address',
                'url'   => 'index.php?app=my_address&act=add',
            ),*/
        );
/*        if (ACT == 'edit')
        {
            $menus[] = array(
                'name' => 'edit_address',
                'url'  => '',
            );
        }*/
        return $menus;
    }
    
    /**
     *    设为默认地址
     * @param int $addr_id 地址ID
     *
     */
    function _set_default_address($addr_id)
    {
        if(!$addr_id)
        {
            return ;
        }
        $model_address =& m('address');
        $model_address->edit("addr_id != {$addr_id} AND is_deafult=1 AND user_id=" . $this->visitor->get('user_id'), array('is_deafult'=>0));
        if ($model_address->has_error())
            {
                $this->show_warning($model_address->get_error());

                return ;
            }
        
    }
}

?>